package com.kefu.robot.service;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.common.context.KefuContext;
import com.kefu.common.exception.BusinessException;
import com.kefu.framework.config.UploadProperties;
import com.kefu.robot.utils.AidataUtil;
import com.kefu.robot.vo.RobotAidataDocInsertVo;
import com.kefu.robot.vo.RobotAidataDocUpdateVo;
import com.kefu.robot.vo.RobotAidataUtilSaveVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kefu.common.service.AbstractKefuService;
import com.kefu.robot.mapper.RobotAidataDocMapper;
import com.kefu.robot.model.RobotAidataDoc;

import java.io.File;


/**
 * @author xiaoyu
 * @date 2023/11/03
 */
@Service
public class RobotAidataDocService extends AbstractKefuService<RobotAidataDoc> {
    @Autowired
    protected RobotAidataDocMapper robotAidataDocMapper;

    @Autowired
    private AidataUtil aidataUtil;

    UploadProperties upload;

    public RobotAidataDocService(UploadProperties upload) {
        this.upload = upload;
    }

    public boolean saveAidataDoc(RobotAidataDocInsertVo robotAidataDocVo) throws Exception{
        if (robotAidataDocVo.getType()==1&&(StringUtils.isBlank(robotAidataDocVo.getUrl()))) {
            throw BusinessException.msg("文件未上传成功");
        }
        RobotAidataDoc info = robotAidataDocVo.convert();
        RobotAidataUtilSaveVo robotAidataUtilSaveVo = new RobotAidataUtilSaveVo();
        robotAidataUtilSaveVo.setDocId(info.getDocId());
        robotAidataUtilSaveVo.setTitle(info.getName());
        robotAidataUtilSaveVo.setContent(info.getContent());
        robotAidataUtilSaveVo.setUrl(info.getUrl());
        robotAidataUtilSaveVo.setFileName(info.getFileName());
        robotAidataUtilSaveVo.setAidataId(info.getAidataId());
        if (robotAidataDocVo.getType() == 0) {
            aidataUtil.docSave(robotAidataUtilSaveVo);
        } else {
            aidataUtil.docFileSave(robotAidataUtilSaveVo);
        }
        return save(info);
    }

    public boolean removeAidataDoc(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            RobotAidataDoc aidataDoc = getById(id);
            if (aidataDoc != null && aidataDoc.getType() == 1 && StringUtils.isNotBlank(aidataDoc.getUrl())) {
                delFile(aidataDoc.getUrl());
            }
            aidataUtil.docDelete(id);
            removeById(id);
        }
        return true;
    }

    public boolean updateAidataDoc(RobotAidataDocUpdateVo robotAidataDocVo) throws Exception{
        RobotAidataDoc info = robotAidataDocVo.convert();
        RobotAidataDoc robotAidataDoc = robotAidataDocMapper.selectOne(Wrappers.lambdaQuery(RobotAidataDoc.class)
                .eq(RobotAidataDoc::getTenantId, KefuContext.getKefuContext().getTenantId())
                .eq(RobotAidataDoc::getDocId, info.getDocId()));
        if (robotAidataDoc == null) {
            throw BusinessException.msg("不存在此题条！");
        }
        RobotAidataUtilSaveVo robotAidataUtilSaveVo = new RobotAidataUtilSaveVo();
        robotAidataUtilSaveVo.setDocId(info.getDocId());
        robotAidataUtilSaveVo.setTitle(info.getName());
        robotAidataUtilSaveVo.setContent(info.getContent());
        robotAidataUtilSaveVo.setUrl(info.getUrl());
        robotAidataUtilSaveVo.setFileName(info.getFileName());
        robotAidataUtilSaveVo.setAidataId(info.getAidataId());

        if (robotAidataDocVo.getType() == 0) {
            aidataUtil.docSave(robotAidataUtilSaveVo);
        } else {
            aidataUtil.docFileSave(robotAidataUtilSaveVo);
        }
        return updateById(info);
    }


    public String getPath(String url) {
        return url.replace(upload.getBaseUrl(), upload.getBasePath());
    }

    public void delFile(String url) {
        String path = getPath(url);
        File file = new File(path);
        FileUtil.del(file);
        logger.info("删除AI模型文件: path {}", path);
    }

}